
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#include "getPortStatus.h"

#if defined(WIN32)
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#endif

int main(int argc, char *argv[])
{
    int optind = 1;

    if (argc < 3) {
        printf("usage e.g.: portping localhost 8080\r\n");
        return FALSE;
    }


#if defined(WIN32)
    WSADATA wsaData;
    int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (iResult != 0) {
        printf("WSAStartup failed with error: %d\n", iResult);
        return 1;
    }
#endif

    int ret = getPortStatus(argv[optind], argv[optind + 1]);
    switch (ret) {
        case CHECK_PORT_USED:
            printf("Check Port : USED\r\n");
            break;
        case CHECK_PORT_NOT_USE:
            printf("Check Port : NOT_USE\r\n");
            break;
        case CHECK_PORT_TIMEOUT:
            printf("Check Port : TIMEOUT\r\n");
            break;
        case CHECK_PORT_NOT_USED_ERROR:
            printf("Check Port : PORT_NOT_USED_ERROR\r\n");
            break;
        case CHECK_PORT_USED_ERROR:
            printf("Check Port : PORT_USED_ERROR\r\n");
            break;
        case CHECK_PORT_COMMON_ERROR:
            printf("Check Port : COMMON_ERROR\r\n");
            break;
        default:
            printf("Check Port : COMMON_ERROR\r\n");
            break;
    }

    if(ret == CHECK_PORT_NOT_USE
    || ret == CHECK_PORT_TIMEOUT) {

        printf("start port : not used\r\n");
    }

    if(ret == CHECK_PORT_USED
    || ret == CHECK_PORT_USED_ERROR) {

        printf("check port used: used\r\n");

    }
    if(ret == CHECK_PORT_COMMON_ERROR
    || ret == CHECK_PORT_NOT_USED_ERROR) {

        printf("check port used: used, tray again!\r\n");

    }
    
#if defined(WIN32)
    WSACleanup();
#endif
    return TRUE;
}
